local ClientOHOH = _G.ClientOHOH;
local M = ClientOHOH.class.new(script, ClientOHOH.files.Client_PlayerComponent(),script.Parent.Name);
local CallBackManager = ClientOHOH.load.CallBackManager();
local EventList = ClientOHOH.load.EventList();

local LanguageHelper= _G.LanguageHelper
local ui_list=ClientOHOH.load.UIManager().ui_list

local TradeGui=ClientOHOH.load.UIManager():CreateUI("TradeGui",require(script.TradeGui))
local TradeSendMsgGui=ClientOHOH.load.UIManager():CreateUI("TradeSendMsgGui",require(script.TradeSendMsgGui))
local TradeReceiveMsgGui=ClientOHOH.load.UIManager():CreateUI("TradeReceiveMsgGui",require(script.TradeReceiveMsgGui))
local HomeGui=ui_list.HomeGui

local PlayerInfoHelper=_G.ClientOHOH.load.PlayerInfoHelper()
local Game_Cfg=ClientOHOH.config.Game
local GlobalTipGui = ClientOHOH.load.UIManager().ui_list.GlobalTipGui;


function M.new(self,...)
	 M(self,...);
end;


function M.ctor(self, parent)
	M.super.ctor(self, parent);

	self.DataBase=self.parent:GetComponent(ClientOHOH.load.Client_PlayerDataComponent());

	self.TradePlayerList={}
	TradeGui:SetDataBase(self.DataBase)
	self:AddProcess(function()
		self:Wait(2)
		while true do

			local Trade= self.parent.main_inst:WaitForChild("Trade")
			self.tradeSetting=Trade:WaitForChild("tradeSetting")
			self.tradeSate=Trade:WaitForChild("tradeSate")

			if self.tradeSetting and self.tradeSate then
				break
			else
				self:Wait(2)
			end
		end
		
		TradeSendMsgGui.TradeSettingEvent:Connect(function(isOpen)
			self:Fire(EventList.set_trade,isOpen)
		end)

		TradeSendMsgGui.SendTradeEvent:Connect(function(userId)
			self:Fire(EventList.SendTradeAsk,userId)
		end)

		TradeSendMsgGui:TradeSetting(self.tradeSetting.Value>0)


		self.tradeSetting.Changed:Connect(function(value)

			TradeSendMsgGui:TradeSetting(self.tradeSetting.Value>0)
		end)

		TradeGui.CancelAction:Connect(function()
			self:Fire(EventList.target_cancel)
		end)

		TradeGui.ReadyAction:Connect(function()
			self:Fire(EventList.trade_ready)
		end)

		TradeGui.ChangeAction:Connect(function(petId,op)
			self:Fire(EventList.trade_change,{[petId]=1},op)
		end)


		game.Players.PlayerAdded:Connect(function(player)
			self:AddTradePlayer(player)
		end)
	
		game.Players.PlayerRemoving:Connect(function(player)
			self:RemoveTradePlayer(player)
		end)

		for _,player in pairs(game.Players:GetPlayers()) do
			self:AddTradePlayer(player)
		end

	end)

	
end;

function M.RemoveTradePlayer(self,player)
	if player==game.Players.LocalPlayer then
		return 
	end

	if self.TradePlayerList[player] then
		TradeSendMsgGui:RemovePlayer(player)
		self.TradePlayerList[player] =nil
	end
	
end

function M.AddTradePlayer(self,player)
	if player==game.Players.LocalPlayer then
		return 
	end

	
	if self.TradePlayerList[player] then
		return 
	end

	self.TradePlayerList[player]=true
	self:AddProcess(function()
		self:Wait(2)
		while true do
			local Trade=player:WaitForChild("Trade")
			local tradeSetting=Trade:WaitForChild("tradeSetting")
			local tradeSate=Trade:WaitForChild("tradeSate")

			if tradeSetting and tradeSate then
				break
			else
				self:Wait(2)
			end
		end
		TradeSendMsgGui:AddPalyer(player)

	end)
end


function M.dtor(self)
	M.super.dtor(self);
end;

M:RegistEvent(EventList.target_ready,function(self)
	TradeGui:TargetReady()
end)
M:RegistEvent(EventList.targetPet_upate,function(self,pets)
	-- local data={}
	-- for k,v in pairs(pets) do
	-- 	for i=1,v do
	-- 		table.insert(data,k)
	-- 	end
	-- end
	TradeGui:UpdateTargetTrade(pets)
end)

M:RegistEvent(EventList.Trading,function(self,userId,tid,had)
	TradeSendMsgGui:Hide()
	local myself=self.DataBase:GetData("pets").had
	PlayerInfoHelper.GetPlayerInfo(userId,function(h,n)
		TradeGui:Show(n,myself,had,tid)
	end)
end)

M:RegistEvent(EventList.target_cancel,function(self,userId,tid)
	TradeGui:Hide()
end)

M:RegistEvent(EventList.SendTradeAsk_Client,function(self,tid,targetUserId)
	PlayerInfoHelper.GetPlayerInfo(targetUserId,function(h,n)
		TradeReceiveMsgGui:Show(n,function(isAccept)
			self:Fire(EventList.AcceptTradeAsk,tid,isAccept)
		end)
	end)
end)

M:RegistEvent(EventList.ready_finish,function(self)
	TradeGui:ReadyFinish()
end)

return M